package com.example.yuejian11.activity;

import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian11.R;
import com.example.yuejian11.bean.IsCollection;
import com.example.yuejian11.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import butterknife.OnClick;

public class CommentActivity extends BaseActivity {

    @BindView(R.id.title_activity)
    TitleView titleView;

    @BindView(R.id.textInputLayout)
    TextInputEditText textInputEditText;


    //
//    Handler handler_activity = new mHandler();
//
//    class mHandler extends Handler {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//
//
//        }
//    }
    Handler handler_activity = new mHandler();

    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            IsCollection isCollection = null;

            try {
                isCollection = JSON.parseObject(msg.obj.toString(), IsCollection.class);
            }catch (Exception e){
                ToastUtils.showShort("获取数据失败");
            }

            if ("0".equals(isCollection.getError())) {
                ToastUtils.showShort("发布成功");
            } else {
                ToastUtils.showShort("发布失败");
            }

        }
    }

    private static final String TAG = "CommentActivity";
    private String sid;

    @OnClick(R.id.comment)
    void submit() {
        EasyHttp.get("yuejian6/public/index.php/json/slideshow/fabuComment")
                .params("sid", sid).params("user_id",uid).params("intro",textInputEditText.getText().toString())
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_comment);
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }
    private String uid;
    private void initView() {
uid=SPUtils.getInstance().getInt("uid",0)+"";
        sid = getIntent().getStringExtra("sid");
        titleView.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(CommentActivity.class);
            }
        });


    }

    private void initData() {

    }
}
